home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / XLayerInfo / layerdemo.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  7KB  |  216 lines

  1. /*
  2.  * Copyright (c) 1993-94, Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  21.  */
  22. /* $Revision: 1.2 $ */
  23. /* compile: cc -o layerdemo layerdemo.c XLayerUtil.c -lX11 -lm */
  24.  
  25. #include <stdio.h>
  26. #include <math.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. #include <GL/gl.h>
  30. #include <GL/glx.h>
  31. #include "XLayerUtil.h"
  32.  
  33. #define SIZE 400 /* width and height of window */
  34.  
  35. Display *dpy;
  36. Window root, win, overlay;
  37. Colormap cmap;
  38. int screen, white, nVisuals, status;
  39. GC overlayGC;
  40. XEvent event;
  41. XLayerVisualInfo template;
  42. XLayerVisualInfo *otherLayerInfo, *defaultLayerInfo;
  43. XSetWindowAttributes swa;
  44. XGCValues gcvals;
  45. XColor color, exact;
  46. int x = 0, y = SIZE/2;
  47. int first_time = 1;
  48. GLXContext cx;
  49. XVisualInfo * normal_vi = NULL;
  50.  
  51. static void
  52. initGL (void)
  53. {
  54.     cx = glXCreateContext(dpy, normal_vi, 0, True);
  55.     glXMakeCurrent(dpy, win, cx);   
  56.  
  57.     glEnable(GL_DEPTH_TEST);
  58.     glDepthFunc(GL_LEQUAL);
  59.     glClearDepth(1.0);
  60.     glClearColor(0.0, 0.0, 0.0, 0.0);
  61.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  62.     glLoadIdentity();
  63.     gluPerspective(40.0, 1.0, 10.0, 200.0);
  64.     glTranslatef(0.0, 0.0, -50.0);
  65.     glRotatef(-58.0, 0.0, 1.0, 0.0);
  66. }
  67.  
  68. redrawNormalPlanes()
  69. {
  70.     printf("%d : redrawNormalPlanes\n", getpid());
  71.     if (first_time) {
  72.         initGL ();
  73.         first_time = 0;
  74.     }
  75.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  76.     glBegin(GL_POLYGON);
  77.     glColor3f(0.0, 0.0, 0.0);
  78.     glVertex3f(-10.0, -10.0, 0.0);
  79.     glColor3f(0.7, 0.7, 0.7);
  80.     glVertex3f(10.0, -10.0, 0.0);
  81.     glColor3f(1.0, 1.0, 1.0);
  82.     glVertex3f(-10.0, 10.0, 0.0);
  83.     glEnd();
  84.     glBegin(GL_POLYGON);
  85.     glColor3f(1.0, 1.0, 0.0);
  86.     glVertex3f(0.0, -10.0, -10.0);
  87.     glColor3f(0.0, 1.0, 0.7);
  88.     glVertex3f(0.0, -10.0, 10.0);
  89.     glColor3f(0.0, 0.0, 1.0);
  90.     glVertex3f(0.0, 5.0, -10.0);
  91.     glEnd();
  92.     glBegin(GL_POLYGON);
  93.     glColor3f(1.0, 1.0, 0.0);
  94.     glVertex3f(-10.0, 6.0, 4.0);
  95.     glColor3f(1.0, 0.0, 1.0);
  96.     glVertex3f(-10.0, 3.0, 4.0);
  97.     glColor3f(0.0, 0.0, 1.0);
  98.     glVertex3f(4.0, -9.0, -10.0);
  99.     glColor3f(1.0, 0.0, 1.0);
  100.     glVertex3f(4.0, -6.0, -10.0);
  101.     glEnd();
  102.     glXSwapBuffers(dpy, win);
  103.     glFlush();
  104. }
  105.  
  106. #define MESSAGE1 "This text is in the"
  107. #define MESSAGE2 "OVERLAY PLANES"
  108.  
  109. redrawOverlayPlanes()
  110. {
  111.    printf("%d : redrawOverlayPlanes\n", getpid());
  112.    XDrawString(dpy, overlay, overlayGC, x, y, MESSAGE1, sizeof(MESSAGE1)-1);
  113.    XDrawString(dpy, overlay, overlayGC, x, y + 15, MESSAGE2, sizeof(MESSAGE2)-1);
  114. }
  115.  
  116. fatalError(message)
  117. char *message;
  118. {
  119.    fprintf(stderr, "layerdemo: %s\n", message);
  120.    exit(1);
  121. }
  122.  
  123. static int attribs[] = {GLX_RGBA, GLX_DOUBLEBUFFER, 
  124.                         GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,
  125.                         GLX_DEPTH_SIZE, 1,
  126.                         None};
  127.                         
  128. main(argc, argv)
  129. int argc;
  130. char *argv[];
  131. {
  132.    dpy = XOpenDisplay(NULL);
  133.    if(dpy == NULL) fatalError("cannot open display");
  134.    
  135.    screen = DefaultScreen(dpy);
  136.    root = RootWindow(dpy, screen);
  137.    normal_vi = glXChooseVisual(dpy, screen, attribs);
  138.  
  139.    cmap = XCreateColormap(dpy, root, normal_vi->visual, AllocNone);
  140.    swa.colormap = cmap;
  141.    swa.event_mask = ExposureMask;
  142.    swa.border_pixel = 0;
  143.    win = XCreateWindow(dpy, root, 10, 10, SIZE, SIZE, 0, normal_vi->depth,
  144.                    InputOutput, normal_vi->visual, 
  145.                    CWColormap | CWEventMask | CWBorderPixel, &swa);
  146.    XStoreName(dpy, win, "Overlay Test");
  147.    XSetWMColormapWindows(dpy, win, &win, 1);
  148.    
  149.    /* find layer of default visual */
  150.    template.vinfo.visualid = normal_vi->visualid; 
  151.    defaultLayerInfo = XGetLayerVisualInfo(dpy, VisualIDMask, &template, &nVisuals);
  152.  
  153.    /* look for visual in layer "above" default visual with transparent pixel */
  154.    template.layer = defaultLayerInfo->layer + 1;
  155.    template.vinfo.screen = screen;
  156.    template.type = TransparentPixel;
  157.    otherLayerInfo = XGetLayerVisualInfo(dpy, 
  158.                                         VisualScreenMask|VisualLayerMask|VisualTransparentType, 
  159.                                         &template, &nVisuals);
  160.  
  161. /*   otherLayerInfo++; */
  162.    
  163.    printf("Overlay visual id is %X\n", otherLayerInfo->vinfo.visualid);
  164.  
  165.       /* XCreateColormap uses AllocNone for 2 reasons:
  166.        * 1) haven't determined class of visual, visual could have static colormap
  167.        * and more importantly
  168.        * 2) transparent pixel might make AllocAll impossible.
  169.        */
  170.    cmap = XCreateColormap(dpy, root, otherLayerInfo->vinfo.visual, AllocNone);
  171.  
  172.       /* not default colormap, must find our own black and white */
  173.    status = XAllocNamedColor(dpy, cmap, "white", &color, &exact);
  174.    if(status == 0) {
  175.        fatalError("could not allocate white");
  176.    }
  177.    white = color.pixel;
  178.    
  179.    swa.border_pixel = white;
  180.    swa.background_pixel = otherLayerInfo->value;
  181.    swa.colormap = cmap;
  182.    swa.event_mask = ExposureMask | ButtonPressMask;    
  183.    overlay = XCreateWindow(dpy, win, 0, 0, SIZE, SIZE, 0, 
  184.                            otherLayerInfo->vinfo.depth,
  185.                        InputOutput, otherLayerInfo->vinfo.visual, 
  186.                        CWColormap | CWEventMask | CWBorderPixel | CWBackPixel, 
  187.                        &swa);
  188.  
  189.    XSetWMColormapWindows(dpy, win, &overlay, 1);
  190.  
  191.    gcvals.foreground = white;
  192.    gcvals.function = GXxor;   
  193.    overlayGC = XCreateGC(dpy, overlay, GCForeground | GCFunction, &gcvals);
  194.  
  195.    XMapSubwindows(dpy, win);   
  196.    XMapWindow(dpy, win);
  197.    
  198.    while(1) {
  199.       XNextEvent(dpy, &event);
  200.       switch(event.type) {
  201.       case Expose:
  202.          if(event.xexpose.window == win)
  203.             redrawNormalPlanes();
  204.          else
  205.             redrawOverlayPlanes();
  206.          break;
  207.       case ButtonPress:
  208.      x = random() % SIZE/2;
  209.      y = random() % SIZE;
  210.      XClearWindow(dpy, overlay);
  211.          redrawOverlayPlanes();
  212.          break;
  213.       }
  214.    }
  215. }
  216.